package com.ztgf.search.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.search.models.bo.SearchUserBO;
import com.ztgf.search.models.bo.SearchUserSaveBO;
import com.ztgf.search.models.dto.SearchUserDTO;
import com.ztgf.search.models.service.ISearchUserService;
import com.ztgf.search.models.vo.SearchUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 搜索引擎系统用户表控制器
 *
 * @author chenyu.wang
 * @Date 2021-01-28 13:50:45
 */

@RestController
@RequestMapping("/cms/searchUser")
@Slf4j
@Api(value = "SearchUserController", tags = "搜索引擎系统用户表接口")
public class SearchUserController {

    @DubboReference
    private ISearchUserService searchUserService;

    @ApiOperation(value = "添加搜索引擎系统用户表", notes = "添加搜索引擎系统用户表")
    @PostMapping(value = "/insert")
    public Result<?> insert(SearchUserSaveBO saveBO) {
        if (StrUtils.isEmpty(saveBO.getAccount()) || StrUtils.isEmpty(saveBO.getPassword()) || saveBO.getPassword().length() != 32) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        // 验证是否有相同的用户名
        Wrapper<SearchUserDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("account", saveBO.getAccount());
        SearchUserDTO dto = searchUserService.selectOne(wrapper);
        if (dto != null) {
            return new Result<>(CodeEnum.FAIL_USER_200002);
        }
        boolean flag = searchUserService.add(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "修改搜索引擎系统用户表", notes = "修改搜索引擎系统用户表")
    @PostMapping("/update")
    public Result<?> update(SearchUserSaveBO saveBO) {
        SearchUserDTO dto = new SearchUserDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        dto.setPassword(null);
        dto.setSalt(null);
        boolean flag = searchUserService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态", notes = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
            , @ApiImplicitParam(name = "status", value = "状态 0启用，1冻结、2删除", required = true, paramType = "query", dataType = "Integer")
    })
    public Result<?> updateStatus(Long id, Integer status) {
        SearchUserDTO dto = new SearchUserDTO();
        dto.setId(id);
        dto.setStatus(status);
        boolean flag = searchUserService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "删除搜索引擎系统用户表", notes = "删除搜索引擎系统用户表")
    @ApiImplicitParam(name = "id", value = "搜索引擎系统用户表id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/delete")
    public Result<?> delete(@RequestParam("id") Long id) {
        boolean flag = searchUserService.deleteById(id);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除搜索引擎系统用户表", notes = "批量删除搜索引擎系统用户表")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result<?> deleteBatch(@RequestParam("ids") String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        searchUserService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<SearchUserDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        SearchUserDTO dto = searchUserService.selectById(id);
        return new Result<>(dto);
    }

    @ApiOperation(value = "搜索引擎系统用户表列表", notes = "搜索引擎系统用户表列表")
    @PostMapping("/queryPageList")
    public Result<Page<SearchUserDTO>> queryPageList(SearchUserBO bo) {
        Wrapper<SearchUserDTO> wrapper = new EntityWrapper<>();
        if (CustomizeUtil.strIsNotNull(bo.getAccount())) {
            wrapper.like("account", bo.getAccount());
        }
        if (CustomizeUtil.strIsNotNull(bo.getName())) {
            wrapper.like("name", bo.getName());
        }
        Page<SearchUserDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = searchUserService.selectPage(page, wrapper);
        List<SearchUserDTO> list = page.getRecords();
        if (CustomizeUtil.listIsNotNull(list)) {
            for (SearchUserDTO dto : list) {
                dto.setPassword(null);
                dto.setSalt(null);
            }
        }
        return new Result<>(page);
    }

    @ApiOperation(value = "修改密码", notes = "修改密码")
    @PostMapping("/updatePassword")
    public Result<?> update(String oldPassword, String newPassword, @RequestParam(value = "name", required = false) String name) {
        if (StrUtils.isEmpty(oldPassword) || StrUtils.isEmpty(newPassword)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return searchUserService.updateUserPassword(oldPassword, newPassword, name);
    }

    @PostMapping("/queryIndexPageList")
    @ApiOperation(value = "搜索引擎管理列表", notes = "搜索引擎管理列表")
    public Result<?> queryIndexPageList(SearchUserBO bo) {
        Page<SearchUserVO> pageList = searchUserService.queryIndexPageList(bo);
        return new Result<>(pageList);
    }
}
